import * as types from '../type';

const state = {
  // 当前门店ID
  store: JSON.parse(sessionStorage.getItem('store')) || {},
  mode: JSON.parse(sessionStorage.getItem('mode')) || {}
};

const getters = {
  store: state => state.store
};
const actions = {
  setStore ({commit}, data) {
    commit(types.SET_STORE, data);
  },
  removeStore () {
    sessionStorage.removeItem('store');
  },
  setMode ({commit}, data) {
    commit(types.SET_MODE, data);
  },
  removeMode ({commit}) {
    commit(types.SET_MODE, {});
    sessionStorage.removeItem('mode');
  }
};
const mutations = {
  [types.SET_STORE] (state, res) {
    state.store.storeCode = res.storeCode;
    state.store.storeName = res.storeName;
    state.store.groupCode = res.groupCode || state.store.groupCode;
    state.store.groupName = res.groupName || state.store.groupName;
    sessionStorage.setItem('store', JSON.stringify(state.store));
  },
  [types.SET_MODE] (state, res) {
    state.mode.modeName = res.modeName;
    state.mode.cityCode = res.cityCode;
    sessionStorage.setItem('mode', JSON.stringify(state.mode));
  }
};

export default {
  state,
  actions,
  getters,
  mutations
};
